package com.stylefeng.guns.rest.modular.film;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.stylefeng.guns.api.film.service.FilmAsyncServiceAPI;
import com.stylefeng.guns.api.film.service.FilmServiceApi;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.rest.modular.film.vo.FilmIndexVO;
import com.stylefeng.guns.rest.modular.vo.ResponseVO;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @创建人 lIUJF
 * @创建时间 2019/1/8
 * @描述:
 */

@RestController
@RequestMapping("/cinema")
public class FilmController {

	private static final String IMG_PRE = "www.baidu.com";

	@Reference(interfaceClass = FilmServiceApi.class)
	private FilmServiceApi filmServiceApi;

	//async = true 开启dubbo异步调用
	@Reference(interfaceClass = FilmAsyncServiceAPI.class, async = true)
	private FilmAsyncServiceAPI filmAsyncServiceAPI;

	//获取首页信息接口。
	/*
	  API网关：
	  	1.功能聚合【API聚合】
	  	好处：
	  		1.六个接口，一次请求，同一时刻节省了五次http请求
	  		2.同一个接口对外暴露，降低了前后端分离开发的难度和复杂度。
	  	坏处：
	  		1.一次获取数据过多，容易出现问题。
	 */
	@RequestMapping(value = "getIndex", method = RequestMethod.GET)
	public ResponseVO getIndex(){
		FilmIndexVO indexVO = new FilmIndexVO();
		//获取banner信息
		indexVO.setBanners(filmServiceApi.getBanners());
		// 获取正在热映的电影
		indexVO.setHotFilms(filmServiceApi.getHotFilms(true, 8, 1, 1, 99, 99, 99));
		// 即将上映的电影
		indexVO.setSoonFilms(filmServiceApi.getSoonFilms(true, 8, 1, 1, 99, 99, 99));
		//票房排行榜
		indexVO.setBoxRanking(filmServiceApi.getBoxRanking());
		//获取受欢迎
		indexVO.setExpectRanking(filmServiceApi.getExpectRanking());
		//获取前一百
		indexVO.setTop100(filmServiceApi.getTop());
		return ResponseVO.success(IMG_PRE, indexVO);
	}

	@RequestMapping(value = "getConditionList", method = RequestMethod.GET)
	public ResponseVO getConitionList(@RequestParam(name = "catId", required = false, defaultValue = "99") String catId, @RequestParam(name = "sourceId", required = false, defaultValue = "99") String sourceId, @RequestParam(name = "yearId", required = false, defaultValue = "99") String yearId){
		FilmConditionVO conditionVO = new FilmConditionVO();
		//类型集合
		boolean flag = false;
		List<CatVO> cats = filmServiceApi.getCats();
		List<CatVO> catResults = new ArrayList<>();
		CatVO catVO = null;
		if (null != cats && cats.size() > 0) {
			for (CatVO cat : cats) {
				//判断集合是否存在catId，存在，则将对应实体变成active状态。
				if (cat.getCatId().equals("99")) {
					catVO = cat;
					continue;
				}
				if (cat.getCatId().equals(catId)) {
					flag = true;
					cat.setActive(true);
				}
				catResults.add(cat);
				//

			}
			if (!flag) {
				catVO = (catVO == null ? new CatVO() : catVO);
				catVO.setActive(true);
				catResults.add(catVO);
			}
		}

		// 片源集合
		flag = false;
		List<SourceVO> sources = filmServiceApi.getSources();
		List<SourceVO> sourceResult = new ArrayList<>();
		SourceVO sourceVO = null;
		for (SourceVO source : sources) {
			if (source.getSourceId().equals("99")) {
				sourceVO = source;
				continue;
			}
			if (source.getSourceId().equals(catId)) {
				flag = true;
				source.setActive(true);
			} else {
				source.setActive(false);
			}
			sourceResult.add(source);
		}
		// 如果不存在，则默认将全部变为Active状态
		if (!flag) {
			sourceVO.setActive(true);
			sourceResult.add(sourceVO);
		} else {
			sourceVO.setActive(false);
			sourceResult.add(sourceVO);
		}

		// 年代集合
		flag = false;
		List<YearVO> years = filmServiceApi.getYears();
		List<YearVO> yearResult = new ArrayList<>();
		YearVO yearVO = null;
		for (YearVO year : years) {
			if (year.getYearId().equals("99")) {
				yearVO = year;
				continue;
			}
			if (year.getYearId().equals(catId)) {
				flag = true;
				year.setActive(true);
			} else {
				year.setActive(false);
			}
			yearResult.add(year);
		}

		// 如果不存在，则默认将全部变为Active状态
		if (!flag) {
			yearVO.setActive(true);
			yearResult.add(yearVO);
		} else {
			yearVO.setActive(false);
			yearResult.add(yearVO);
		}
		conditionVO.setCatInfo(catResults);
		conditionVO.setSourceInfo(sourceResult);
		conditionVO.setYearInfo(yearResult);
		return ResponseVO.success(IMG_PRE, conditionVO);
	}

	@RequestMapping(value = "getFilms", method = RequestMethod.GET)
	public ResponseVO getFilms(FilmRequestVO filmRequestVO){
		String img_pre = "http://img.meetingshop.cn/";
		FilmVO filmVO = null;
		// 根据showType判断影片查询类型
		switch (filmRequestVO.getShowType()) {
			case 1:
				filmVO = filmServiceApi.getHotFilms(false, filmRequestVO.getPageSize(), filmRequestVO.getNowPage(), filmRequestVO.getSortId(), filmRequestVO.getSourceId(), filmRequestVO.getYearId(), filmRequestVO.getCatId());
				break;
			case 2:
				filmVO = filmServiceApi.getSoonFilms(false, filmRequestVO.getPageSize(), filmRequestVO.getNowPage(), filmRequestVO.getSortId(), filmRequestVO.getSourceId(), filmRequestVO.getYearId(), filmRequestVO.getCatId());
				break;
			case 3:
				filmVO = filmServiceApi.getClassicFilms(filmRequestVO.getPageSize(), filmRequestVO.getNowPage(), filmRequestVO.getSortId(), filmRequestVO.getSourceId(), filmRequestVO.getYearId(), filmRequestVO.getCatId());
				break;
			default:
				filmVO = filmServiceApi.getHotFilms(false, filmRequestVO.getPageSize(), filmRequestVO.getNowPage(), filmRequestVO.getSortId(), filmRequestVO.getSourceId(), filmRequestVO.getYearId(), filmRequestVO.getCatId());
				break;
		}
		// 根据sortId排序
		// 添加各种条件查询
		// 判断当前是第几页

		return ResponseVO.success(filmVO.getNowPage(), filmVO.getTotalPage(), img_pre, filmVO.getFilmInfo());
	}


	/***
	 * @param searchParam
	 * @param searchType(按ID查，则去redis或者memrtchche去缓存查，或者Mysql查,按名称查，则去搜索引擎查找,ele..serach)
	 * @return
	 * @throws InterruptedException
	 * @throws java.util.concurrent.ExecutionException
	 */
	@RequestMapping(value = "films/{searchParam}", method = RequestMethod.GET)
	public ResponseVO films(@PathVariable("searchParam") String searchParam, int searchType){
		// 根据searchType，判断查询类型
		FilmDetailVO filmDetail = filmServiceApi.getFilmDetail(searchType, searchParam);
		if (filmDetail == null) {
			return ResponseVO.serviceFail("没有可查询的影片");
		} else if (filmDetail.getFilmId() == null || filmDetail.getFilmId().trim().length() == 0) {
			return ResponseVO.serviceFail("没有可查询的影片");
		}
		String filmId = filmDetail.getFilmId();
		filmAsyncServiceAPI.getFilmDesc(filmId);
		Future<FilmDescVO> filmDescVOFuture = RpcContext.getContext().getFuture();


		filmAsyncServiceAPI.getImgs(filmId);
		Future<ImgVO> imgVOFuture = RpcContext.getContext().getFuture();

		filmAsyncServiceAPI.getDectInfo(filmId);
		Future<ActorVO> actorVOFuture = RpcContext.getContext().getFuture();

		filmAsyncServiceAPI.getActors(filmId);
		Future<List<ActorVO>> actorVOFuture1 = RpcContext.getContext().getFuture();

		/*// 查询影片的详细信息 -> Dubbo的异步调用
		// 获取影片描述信息
		//filmAsyncServiceApi.getFilmDesc(filmId);
		Future<FilmDescVO> filmDescVOFuture = RpcContext.getContext().getFuture();
		// 获取图片信息
		//filmAsyncServiceApi.getImgs(filmId);
		Future<ImgVO> imgVOFuture = RpcContext.getContext().getFuture();
		// 获取导演信息
		//filmAsyncServiceApi.getDectInfo(filmId);
		Future<ActorVO> actorVOFuture = RpcContext.getContext().getFuture();
		// 获取演员信息
		//filmAsyncServiceApi.getActors(filmId);
		Future<List<ActorVO>> actorsVOFutrue = RpcContext.getContext().getFuture();*/
		// 组织info对象
		InfoRequstVO infoRequstVO = new InfoRequstVO();
		// 组织Actor属性
		ActorRequestVO actorRequestVO = new ActorRequestVO();
		try {
			actorRequestVO.setActors(actorVOFuture1.get());
			actorRequestVO.setDirector(actorVOFuture.get());
			infoRequstVO.setActors(actorRequestVO);
			infoRequstVO.setBiography(filmDescVOFuture.get().getBiography());
			infoRequstVO.setFilmId(filmId);
			infoRequstVO.setImgVO(imgVOFuture.get());
		} catch (Exception e) {
			e.printStackTrace();
		}
		//组织成返回值
		filmDetail.setInfo04(infoRequstVO);
		return ResponseVO.success("http://img.meetingshop.cn/", filmDetail);
	}
}
